//
//  SIXMeSettingVC.m
//  weibo
//
//  Created by YaRi on 16/5/21.
//  Copyright © 2016年 chuhan. All rights reserved.
//

#import "SIXMeSettingVC.h"
#import "CleanCachesHelper.h"
#import "MBProgressHUD+XMG.h"

@interface SIXMeSettingVC ()

@end

@implementation SIXMeSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置";
    
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        return 2;
    }
    if (section == 1 || section == 2) {
        return 3;
    }
    
        return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSInteger section = indexPath.section;
        NSInteger row = indexPath.row;
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            switch (section) {
                case 0:
                    if(row == 0)
                    {
                        cell.textLabel.text =  @"账号管理";
                    }
                    if (row ==1) {
                        cell.textLabel.text =  @"账号安全";
                        
                    }
                    
                    break;
                case 1:
                    if(row == 0)
                    {
                        cell.textLabel.text =  @"通知";
                    }
                    if (row ==1) {
                        cell.textLabel.text =  @"隐私与安全";
                        
                    }
                    if (row ==2) {
                        cell.textLabel.text =  @"通用设置";
                        
                    }
                    break;
                case 2:
                    if(row == 0)
                    {
                        cell.textLabel.text =  @"清理缓存";
                        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.0fMB", [CleanCachesHelper folderSizeAtPath:[CleanCachesHelper getCachesPath:@"Caches"]]];
                        

                    }
                    if (row ==1) {
                        cell.textLabel.text =  @"意见反馈";
                        
                    }
                    if (row ==2) {
                        cell.textLabel.text =  @"关于微博";
                        
                    }
                    break;
                case 3:
                    if(row == 0)
                    {
                        cell.textLabel.text =  @"退出当前账号";
                        cell.textLabel.textAlignment = NSTextAlignmentCenter;
                        cell.textLabel.textColor = [UIColor redColor];
                    }
                    break;
                default:
                    break;
            }
        }
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;

    switch (section) {
        case 0:
            if(row == 0)
            {
                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor redColor];
                vc.title = @"账号管理";
                [self.navigationController pushViewController:vc animated:YES];
            }
            if (row ==1) {

                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor greenColor];
                  vc.title = @"账号安全";
                [self.navigationController pushViewController:vc animated:YES];
            }
            
            break;
        case 1:
            if(row == 0)
            {
                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor redColor];
                  vc.title = @"通知";
                [self.navigationController pushViewController:vc animated:YES];
            }
            if (row ==1) {

                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor greenColor];
                  vc.title = @"隐私与安全";
                [self.navigationController pushViewController:vc animated:YES];
            }
            if (row ==2) {

                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor redColor];
                  vc.title = @"通用设置";
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        case 2:
            if(row == 0)
            {
                [self pop];
            }
            if (row ==1) {

                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor redColor];
                  vc.title = @"意见反馈";
                [self.navigationController pushViewController:vc animated:YES];
            }
            if (row ==2) {

                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor greenColor];
                  vc.title = @"关于微博";
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        case 3:
            if(row == 0)
            {
                UIViewController *vc = [[UIViewController alloc] init];
                vc.view.backgroundColor = [UIColor redColor];
                  vc.title = @"退出当前账号";
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        default:
            break;
    }

    
}
//弹出
- (void)pop{
    //第一步:创建控制器
    UIAlertController *alerVC = [[UIAlertController alloc] init];
    
    //第二步:创建按钮
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
            [MBProgressHUD showMessage:@"正在清理缓存..." ];
        
            [CleanCachesHelper cleanCaches:[CleanCachesHelper getCachesPath:@"Caches"]];
        
        //延时执行代码
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUD];
            [MBProgressHUD showSuccess:@"缓存清理完毕"];
//            [self.tableView reloadData];
            NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
            
//            [self.tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
            [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        });

    }];
    
    //第三步,添加按钮
    [alerVC addAction:action1];
    [alerVC addAction:action2];

    //第四步:显示弹窗.(相当于show操作)
    [self presentViewController:alerVC animated:YES completion:nil ];
    
}

@end
